
import { app } from "../../scripts/app.js";
import { ComfyWidgets } from "../../scripts/widgets.js";

app.registerExtension({
	name: "destiny",
    async beforeRegisterNodeDef(nodeType, nodeData, app) {
        const onNodeCreated = nodeType.prototype.onNodeCreated;
        if (nodeData.python_module.indexOf('destiny') != -1) {
            if (nodeData.name == "DisplayInfo") {
                nodeType.prototype.onNodeCreated = function () {
                    onNodeCreated ? onNodeCreated.apply(this, []) : undefined;
                    this.showValueWidget = ComfyWidgets["STRING"](this, "output", ["STRING", { multiline: true }], app).widget;
                    this.showValueWidget.inputEl.readOnly = true;
                    this.showValueWidget.element.style.fontSize = "12px"
                };
                const onExecuted = nodeType.prototype.onExecuted;
                nodeType.prototype.onExecuted = function (message) {
                    onExecuted === null || onExecuted === void 0 ? void 0 : onExecuted.apply(this, [message]);
                    const texts = message.text
                    if (!texts) return
                    let text = ""
                    for (let i=0;i<texts.length;i++) {
                        text += `${texts[i]}\n`
                    }
                    // console.log('this.showValueWidget', this.showValueWidget)
                    this.showValueWidget.value = text
                    const widgetElementStyle = this.showValueWidget.element.style
                    // console.log('widgetElementStyle', widgetElementStyle)
                    widgetElementStyle.fontSize = `${message.size[0]}px`
                    widgetElementStyle.color = `${message.color[0]}`
                };
            } else if (nodeData.name == "LabelCombine") {
                nodeType.prototype.onNodeCreated = function () {
                    onNodeCreated ? onNodeCreated.apply(this, []) : undefined;
                    this.addInput('label_1', 'STRING')
                    // this.addInput('label_2', 'STRING')
                    const onConnectInput = this.onConnectInput
                    this.onConnectInput = function(index, type, slot) {
                        onConnectInput?.apply(this, [])
                        // console.log(index, type, slot, this)
                        if (index == this.inputs.length - 1) {
                            this.addInput(`label_${this.inputs.length + 1}`, 'STRING')
                        }
                    }
                };
                const onConnectionsChange = nodeType.prototype.onConnectionsChange;
                nodeType.prototype.onConnectionsChange = function (side, slot, connect, link_info, output) {     
                    onConnectionsChange?.apply(this, arguments);
                    if (!connect && side == 1) {
                        const inputs = this.inputs
                        let last_index = inputs.length - 1
                        let prev_index = last_index - 1
                        while (prev_index >= 0 && !inputs[last_index].link && !inputs[prev_index].link) {
                            this.removeInput(last_index)
                            last_index = inputs.length - 1
                            prev_index = last_index - 1
                        }

                        // this.removeInput(slot + 1)
                        
                        // if (inputs && slot == inputs.length - 2 && !inputs[inputs.length-1].link) {
                        //     this.removeInput(slot + 1)
                        // }
                    }
                }
            }
        }
    }
})
